package com.ubst.smartad.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSASchedulerDO;
import com.ubst.smartad.logger.Debug;

public class TSASchedulerMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;
	
	/** 
     * 생성자
     *   
    */ 
	public TSASchedulerMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSASchedulerMngImpl", log_level);
	}
	
	/**
	 * 시스템정보를 가져오기 위한 예약작업 등록
	 * @param schedulerDO
	 * @throws SQLException
	 */
	public void insertScheduler(TSASchedulerDO schedulerDO) throws SQLException {
		PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_SCHEDULER " )
				.append(" ( ")
				.append("  SCHEDID ,JOBTYPE ,RUNCYCLE ,RUNCNT ,RUNDATE ")
				.append(" ,RUNWEEK ,RUNDONE ,USEYN ,CRESULT ,RUNDESC ")
				.append(" ,CREATEDATE ,LASTDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ")
				.append(" ) ");

			debug.debug("insertScheduler ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, schedulerDO.schedid);
			pstmt.setInt(idx++, schedulerDO.jobtype);
			pstmt.setInt(idx++, schedulerDO.runcycle);
			pstmt.setInt(idx++, schedulerDO.runcnt);
			pstmt.setString(idx++, schedulerDO.rundate);
			pstmt.setInt(idx++, schedulerDO.runweek);
			pstmt.setString(idx++, schedulerDO.rundone);
			pstmt.setString(idx++, schedulerDO.useyn);
			pstmt.setString(idx++, schedulerDO.cresult);
			pstmt.setString(idx++, schedulerDO.rundesc);
			pstmt.setString(idx++, schedulerDO.createdate);
			pstmt.setString(idx++, schedulerDO.lastdate);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;
        } catch (SQLException e){
			throw e;
        } finally {
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }
	}
	
	/**
	 * 시스템정보를 가져오기 위한 예약작업 수정
	 * @param schedulerDO
	 * @throws SQLException
	 */
	public void updateScheduler(TSASchedulerDO schedulerDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_SCHEDULER " )
				.append(" SET ")
				.append("  SCHEDID=? ,JOBTYPE=? ,RUNCYCLE=? ,RUNCNT=? ,RUNDATE=? ")
				.append(" ,RUNWEEK=? ,RUNDONE=? ,USEYN=? ,CRESULT=? ,RUNDESC=? ")
				.append(" ,CREATEDATE=? ,LASTDATE=? ")
				.append(" WHERE SCHEDID=? ");

			debug.debug("updateScheduler ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, schedulerDO.schedid);
			pstmt.setInt(idx++, schedulerDO.jobtype);
			pstmt.setInt(idx++, schedulerDO.runcycle);
			pstmt.setInt(idx++, schedulerDO.runcnt);
			pstmt.setString(idx++, schedulerDO.rundate);
			pstmt.setInt(idx++, schedulerDO.runweek);
			pstmt.setString(idx++, schedulerDO.rundone);
			pstmt.setString(idx++, schedulerDO.useyn);
			pstmt.setString(idx++, schedulerDO.cresult);
			pstmt.setString(idx++, schedulerDO.rundesc);
			pstmt.setString(idx++, schedulerDO.createdate);
			pstmt.setString(idx++, schedulerDO.lastdate);
			pstmt.setInt(idx++, schedulerDO.schedid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
	/**
	 * 시스템정보를 가져오기 위한 예약작업 목록 조회
	 * @param userid
	 * @return
	 * @throws SQLException
	 */
	public TSASchedulerDO selectScheduler(int schedid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSASchedulerDO schedulerDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  SCHEDID ,JOBTYPE ,RUNCYCLE ,RUNCNT ,RUNDATE ")
				.append(" ,RUNWEEK ,RUNDONE ,USEYN ,CRESULT ,RUNDESC ")
				.append(" ,CREATEDATE ,LASTDATE ")
				.append(" FROM TSA_SCHEDULER ")
				.append(" WHERE SCHEDID=? ");

			debug.debug("selectSchedulerList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, schedid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				schedulerDO = new TSASchedulerDO();
				schedulerDO.schedid = schedid;
				schedulerDO.jobtype = rs.getInt("JOBTYPE");
				schedulerDO.runcycle = rs.getInt("RUNCYCLE");
				schedulerDO.runcnt = rs.getInt("RUNCNT");
				schedulerDO.rundate = rs.getString("RUNDATE");
				schedulerDO.runweek = rs.getInt("RUNWEEK");
				schedulerDO.rundone = rs.getString("RUNDONE");
				schedulerDO.useyn = rs.getString("USEYN");
				schedulerDO.cresult = rs.getString("CRESULT");
				schedulerDO.rundesc = rs.getString("RUNDESC");
				schedulerDO.createdate = rs.getString("CREATEDATE");
				schedulerDO.lastdate = rs.getString("LASTDATE");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return schedulerDO;
	}
	
	/**
	 * 시스템정보를 가져오기 위한 예약작업 삭제
	 * @param userid
	 * @throws SQLException
	 */
	public void deleteScheduler(int schedid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_SCHEDULER " )
				.append(" WHERE SCHEDID=? ");

			debug.debug("deleteScheduler ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, schedid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
}
